package com.example.music08;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.bawei.lrcutils.YaoLrcHelper;
import com.bumptech.glide.Glide;
import com.example.music08.entity.Music;
import com.example.music08.utils.MusicUtils;
import com.example.music08.utils.SpUtils;
import com.example.music08.utils.SqlUtils;
import com.lauzy.freedom.library.Lrc;
import com.lauzy.freedom.library.LrcView;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity2 extends AppCompatActivity {

    private ImageView back;
    private TextView name;
    private TextView singer;
    private ImageView picUrl;
    private LrcView lrc;
    private ImageView dianzan;
    private ImageView xiazai;
    private ImageView pinglun;
    private TextView cur;
    private SeekBar bar;
    private TextView dur;
    private ImageView tui;
    private ImageView pre;
    private ImageView pOrp;
    private ImageView next;
    private ImageView jin;
    private ImageView menu;
    //我们需要在进入界面时，先判断歌曲是否被点赞过，如果被点赞--显示对应点赞的图片，再次点击执行取消点赞的操作    未点赞过---显示对应未点赞的图片，再次点击执行点赞的操作
    private Boolean dzFlag = false;//点赞的标志，用来显示是否被点赞---默认是未点赞状态
    //定时器 ---歌词和进度条动起来
    Timer timer;
    Handler handler = new Handler(){};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        initView();
        change();//改变页面上的数据
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        //进度条左右两端的文字
                        cur.setText(MusicUtils.getInstance().getCur());
                        dur.setText(MusicUtils.getInstance().getDur());
                        //进度条的值
                        bar.setMax(MusicUtils.getInstance().getMax());
                        bar.setProgress(MusicUtils.getInstance().getProgress());
                        //歌词跟着变化
                        lrc.updateTime(MusicUtils.getInstance().getProgress());
                    }
                });
            }
        },0,100);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();//直接关闭当前activity,上个页面的fragment不受影响
            }
        });
        tui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MusicUtils.getInstance().tui();
            }
        });
        pre.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MusicUtils.getInstance().pre();
                change();
                sendBroadcast(new Intent("com.bw.pre.main2"));
            }
        });
        pOrp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MusicUtils.getInstance().pOrp(pOrp);
                sendBroadcast(new Intent("com.bw.pOrp.main2"));
            }
        });
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MusicUtils.getInstance().next();
                sendBroadcast(new Intent("com.bw.next.main2"));
                change();
            }
        });
        jin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MusicUtils.getInstance().jin();
            }
        });
        //进度条拖拽  歌词拖拽
        bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if(fromUser){
                    MusicUtils.getInstance().seekTo(progress);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        lrc.setOnPlayIndicatorLineListener(new LrcView.OnPlayIndicatorLineListener() {
            @Override
            public void onPlay(long time, String content) {
                MusicUtils.getInstance().seekTo((int) time);
            }
        });
        //调整播放模式
        menu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //弹出popupmenu

                PopupMenu popupMenu = new PopupMenu(MainActivity2.this, menu);
                popupMenu.inflate(R.menu.pop_menu);
                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        switch (item.getItemId()){
                            case R.id.item01:
                                Toast.makeText(MainActivity2.this, "当前播放模式为顺序播放", Toast.LENGTH_SHORT).show();
                                //更新工具类中的playmode的值
                                MusicUtils.getInstance().setPlayMode(0);
                                SpUtils.getInstance(MainActivity2.this).putInt("playMode",0);//缓存一下，下次打开时是最后选中的播放模式
                                break;
                            case R.id.item02:
                                Toast.makeText(MainActivity2.this, "当前播放模式为随机播放", Toast.LENGTH_SHORT).show();
                                //更新工具类中的playmode的值
                                MusicUtils.getInstance().setPlayMode(1);
                                SpUtils.getInstance(MainActivity2.this).putInt("playMode",1);//缓存一下，下次打开时是最后选中的播放模式
                                break;
                            case R.id.item03:
                                Toast.makeText(MainActivity2.this, "当前播放模式为单曲循环", Toast.LENGTH_SHORT).show();
                                //更新工具类中的playmode的值
                                MusicUtils.getInstance().setPlayMode(2);
                                SpUtils.getInstance(MainActivity2.this).putInt("playMode",2);//缓存一下，下次打开时是最后选中的播放模式
                                break;
                        }
                        return false;
                    }
                });
                popupMenu.show();
            }
        });
        dianzan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(dzFlag){//已点赞
                    SqlUtils.getInstance(MainActivity2.this).delete("dz",MusicUtils.getInstance().getId()+"");
                    dianzan.setImageResource(R.drawable.baseline_favorite_border_24);//图标变更---标志值修改
                    dzFlag = false;
                }else {
                    //保存到数据库中
                    //MusicUtils.getInstance().getIndex()获取当前歌曲的下标
                    //MusicUtils.getInstance().getList().get(i)根据下标获取Music.dataBean---弃用
                    SqlUtils.getInstance(MainActivity2.this).insert(MusicUtils.getInstance().getDataBean(), "dz");
                    dianzan.setImageResource(R.drawable.baseline_favorite_24);//变更图标
                    dzFlag = true;
                }

            }
        });
        xiazai.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity2.this, MyIntentService.class);
                intent.putExtra("path",MusicUtils.getInstance().getData());
                intent.putExtra("file","/sdcard/"+MusicUtils.getInstance().getName()+".mp3");
                startService(intent);
            }
        });
    }

    private void change() {
        name.setText(MusicUtils.getInstance().getName());
        singer.setText(MusicUtils.getInstance().getSinger());
        Glide.with(this).load(MusicUtils.getInstance().getPicUrl()).into(picUrl);
        if(MusicUtils.getInstance().isPlay()){
            pOrp.setImageResource(R.drawable.baseline_pause_circle_outline_24);
        }else{
            pOrp.setImageResource(R.drawable.baseline_play_circle_outline_24);
        }
        YaoLrcHelper.parseUrl(MusicUtils.getInstance().getLrc(), new YaoLrcHelper.LrcCallBack() {
            @Override
            public void getLrcData(List<Lrc> list) {
                lrc.setLrcData(list);
            }
        });
        MusicUtils.getInstance().setPlayMode(SpUtils.getInstance(this).getInt("playMode"));//设置播放模式
        //加载是否点赞的状态---查询数据库表中是否存在这条记录
        List<Music.DataBean> query = SqlUtils.getInstance(this).query("dz", "id", MusicUtils.getInstance().getId() + "");
        if(query.size()>0){
            dzFlag = true;//当前是点赞状态
            dianzan.setImageResource(R.drawable.baseline_favorite_24);
        }else {
            dzFlag = false;
            dianzan.setImageResource(R.drawable.baseline_favorite_border_24);
        }
        //把他们加进最近听过的歌的表中
        SqlUtils.getInstance(this).insert(MusicUtils.getInstance().getDataBean(), "tg");

    }

    private void initView() {
        back = (ImageView) findViewById(R.id.back);
        name = (TextView) findViewById(R.id.name);
        singer = (TextView) findViewById(R.id.singer);
        picUrl = (ImageView) findViewById(R.id.picUrl);
        lrc = (LrcView) findViewById(R.id.lrc);
        dianzan = (ImageView) findViewById(R.id.dianzan);
        xiazai = (ImageView) findViewById(R.id.xiazai);
        pinglun = (ImageView) findViewById(R.id.pinglun);
        cur = (TextView) findViewById(R.id.cur);
        bar = (SeekBar) findViewById(R.id.bar);
        dur = (TextView) findViewById(R.id.dur);
        tui = (ImageView) findViewById(R.id.tui);
        pre = (ImageView) findViewById(R.id.pre);
        pOrp = (ImageView) findViewById(R.id.pOrp);
        next = (ImageView) findViewById(R.id.next);
        jin = (ImageView) findViewById(R.id.jin);
        menu = (ImageView) findViewById(R.id.menu);
    }
}